package com.gsc.config;

import com.gsc.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * @author gscstart
 * @create 2023/2/3-15:58
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    @Bean
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    /**
     * 将登录校验过滤器配置到security过滤器链中
     */

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    /**
     * 注入认证失败处理器
     */
    @Autowired
    AuthenticationEntryPoint authenticationEntryPoint;

    /**
     * 注入授权失败处理器
     */
    @Autowired
    AccessDeniedHandler accessDeniedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                // 对于登录接口 允许匿名访问
                .antMatchers("/user/login").anonymous()
                //退出登录接口，需要携带token访问
//                .antMatchers("/logout").authenticated()
                //测试友链接口需要登录认证 才可以访问(不注销的话，刷新一次页面就要重新登录一次)
//                .antMatchers("/link/getAllLink").authenticated()
                //配置个人信息认证
//                .antMatchers("/user/userInfo").authenticated()
                //文件上传接口需要认证
//                .antMatchers("/upload").authenticated()
                // 所有的请求全部需要认证才能访问
                .anyRequest().authenticated();

        //配置异常处理器
        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        http.logout().disable();

        //将登录校验过滤器 添加到UsernamePasswordAuthenticationFilter  之前
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);

        //允许跨域
        http.cors();
    }
    @Bean
    public PasswordEncoder passwordEncoder(){
        //数据库中使用BCryptPasswordEncoder进行加密处理
        return new BCryptPasswordEncoder();
    }
}
